# -*- coding: utf-8 -*-

"""
=============================
this is a feibot product,
all rights reserved.
www.feibot.com
@author: Gu Tianyuan
@email: g@feibot.com
@time: ${DATE}:${TIME}
@product: ${PRODUCT_NAME}
=============================
"""
import os

from gtyConfig import systemConfig, configFileHandler


# 精确到ms的时间父类
class timeInMs:
    def __init__(self, year=1, month=1, day=1, hour=0, minute=0, second=0, ms=0):
        self.year = year
        self.month = month
        self.day = day
        self.hour = hour
        self.minute = minute
        self.second = second
        self.ms = ms

    def setValues(self, year=1, month=1, day=1, hour=0, minute=0, second=0, ms=0):
        self.year = year
        self.month = month
        self.day = day
        self.hour = hour
        self.minute = minute
        self.second = second
        self.ms = ms


class hardwareTime(timeInMs):
    def __init__(self):
        super().__init__()


class linuxTime(timeInMs):
    def __init__(self):
        super().__init__()
        self.secondCounter = 0


class Channel:
    def __init__(self):
        self.open = 0
        self.tagReadNum = 0
        self.testTagReadNum = 0
        self.chBtn = None
        self.enabled = False

    def resetNum(self):
        self.tagReadNum = 0
        self.testTagReadNum = 0


class Channels:
    def __init__(self):
        self.ch1 = Channel()
        self.ch2 = Channel()
        self.ch3 = Channel()
        self.ch4 = Channel()
        self.ch5 = Channel()
        self.ch6 = Channel()
        self.ch7 = Channel()
        self.ch8 = Channel()
        self.proChannel1 = Channel()
        self.proChannel2 = Channel()
        self.proChannel3 = Channel()
        self.proChannel4 = Channel()
        self.proChannel5 = Channel()
        self.proChannel6 = Channel()
        self.proChannel7 = Channel()
        self.proChannel8 = Channel()
        self.activeChannel = Channel()
        self.innerChs = [self.ch1, self.ch2, self.ch3, self.ch4, self.ch5, self.ch6, self.ch7, self.ch8]
        self.proChs = [self.proChannel1,
                            self.proChannel2,
                            self.proChannel3,
                            self.proChannel4,
                            self.proChannel5,
                            self.proChannel6,
                            self.proChannel7,
                            self.proChannel8]
        self.allChs = self.innerChs + self.proChs
        #self.allChs.append(self.activeChannel)
        self.testNumDisplayTilSecond = 0  # 运行的秒数
        self.displayChannelNames = True
        for i in self.innerChs:
            i.enabled = True
        for i in self.proChs:
            i.enabled = True

    def setBtns(self, btnList):
        self.ch1.chBtn = btnList[0]
        self.ch2.chBtn = btnList[1]
        self.ch3.chBtn = btnList[2]
        self.ch4.chBtn = btnList[3]
        self.ch5.chBtn = btnList[4]
        self.ch6.chBtn = btnList[5]
        self.ch7.chBtn = btnList[6]
        self.ch8.chBtn = btnList[7]
        self.proChannel1.chBtn = btnList[7 + 1]
        self.proChannel2.chBtn = btnList[7 + 2]
        self.proChannel3.chBtn = btnList[7 + 3]
        self.proChannel4.chBtn = btnList[7 + 4]
        self.proChannel5.chBtn = btnList[7 + 5]
        self.proChannel6.chBtn = btnList[7 + 6]
        self.proChannel7.chBtn = btnList[7 + 7]
        self.proChannel8.chBtn = btnList[7 + 8]
        self.activeChannel.chBtn = btnList[7 + 8 + 1]
        self.btnStyleSet()
        # 激活有源通道按钮
        self.activeChannel.chBtn.setStyleSheet(systemConfig.param.btnEnabledStyle)

    def resetNum(self):
        for i in self.allChs:
            i.resetNum()
        self.testNumDisplayTilSecond = 0  # linux整数秒计数小于这个时间时，显示测试卡信息
        self.displayChannelNames = True

    def hide(self, hideCmd=True,target='all'):
        chs = []
        if target == 'all':
            chs = self.allChs+[self.activeChannel]
        if target == 'innerChannels':
            chs = self.allChs[0:8]
        if target == 'pro':
            chs = self.allChs[8:16]
        if target == 'active':
            chs = [self.activeChannel]
        for i in chs:
            if hideCmd:
                i.chBtn.hide()
            else:
                i.chBtn.show()

    def btnStyleSet(self):
        for i in self.allChs:
            if i.enabled:
                i.chBtn.setStyleSheet(systemConfig.param.btnEnabledStyle)
            else:
                i.chBtn.setStyleSheet(systemConfig.param.btnDisabledStyle)


class ConfigHandlers:
    def __init__(self):
        self.machine = configFileHandler.UserConfigHandler()
        self.systemMachine = configFileHandler.ConfigFileHandler(systemConfig.machineConfigFilePath)
        self.state = configFileHandler.ConfigFileHandler(systemConfig.runStateFilePath)
        self.event = configFileHandler.ConfigFileHandler(systemConfig.getEventConfigFilePath())
        self.mmc = configFileHandler.ConfigFileHandler(systemConfig.configFileEmmc)


class tagDataSummary:
    def __init__(self):
        self.totalCheckedNum = 0
        self.differentCheckedNum = 0
        self.checkedTagDict = {}

    def reset(self):
        self.totalCheckedNum = 0
        self.differentCheckedNum = 0
        self.checkedTagDict = {}


class tag:
    def __init__(self):
        self.epcString = ""
        self.epcByteList = []

        self.epcByteNum = 0

        self.hardwareTime = hardwareTime()
        self.hardwareTimeString = ""
        self.hardwareDateString = ""
        self.timeStamp = 0

        self.channelId = 0
        self.rssi = None

        self.bat = 0

    def buildEpcString(self):
        self.epcString = ""
        for i in self.epcByteList:
            self.epcString += '%02x' % i

    def buildTimeStamp(self):
        self.timeStamp = self.hardwareTime.hour * 3600 + self.hardwareTime.minute * 60 + \
                         self.hardwareTime.second + self.hardwareTime.ms / 1000

    def buildTimeString(self):
        self.hardwareTimeString = '%02d:%02d:%02d.%03d' % \
                                  (self.hardwareTime.hour,
                                   self.hardwareTime.minute,
                                   self.hardwareTime.second,
                                   self.hardwareTime.ms)
        self.hardwareDateString = "%04d-%02d-%02d" % (
            self.hardwareTime.year,
            self.hardwareTime.month,
            self.hardwareTime.day)


class machineState:
    def __init__(self):
        self.machineId = ''
        self.eventId = ''
        self.internetConnectionState = 'disconnected'  # 分为《已联网》和《未联网》两种状态

        self.batteryPercentage = -1  # 为一个整数

        self.gunStartTime = None
        self.gunStartTimeString = None

        self.readerState1 = 'stopped'  # 读写器1状态，分为《正在读取》和《停止读取》两种状态
        self.readerState2 = 'stopped'  # 读写器2状态，分为《正在读取》和《停止读取》两种状态
        self.readerState3 = 'stopped'
        self.readerState4 = 'stopped'
        self.readerFreqBand = 0

        self.textBrowserLineNum = 0  # log输出的行数

        # 磁盘可用容量
        self.storageSpace = ''
        self.storageSpaceUnusedG = 0

        # 软件升级提示
        self.updateNotice = ''

        # 提示栏显示
        self.noticeLabelDict = {}

        # 门槛时间
        self.gateTime = 0

        # 允许的epc长度
        self.allowEpcByteNumList = []

        # 最后，截取EPC的最后几个字符。当为0时不截取。
        self.truncateEpcByteNum = 0

        # 切换主界面显示按钮或者log输出
        self.displayChannels = False


class auxiliaryVariables:
    def __init__(self):
        self.gunShowColon = 0
        self.counterPer100ms = 0

        self.getFirmwareFlag = 0
        self.compileVersion = 0
        self.rawEpcNum = 0

        self.enableSocketToServer = 0

        self.gitBranch = ''
        self.gitCommitId = ''
        self.remoteGitMasterVersion = ''
        self.remoteGitDevVersion = ''

        self.hardwareType = 'F2000'

        self.reader1Power = 0
        self.reader2Power = 0
        self.reader3Power = 0
        self.reader4Power = 0
        self.activeReaderPower = 0

        self.beepEnable = 1

        # 从服务器设置时间日期的次数
        self.setDateTimeFromWebTimes = 0

        # 上一次刷新显示的linux时间
        self.lastUpdateDisplayLinuxTime = 0

        # epc显示的缓冲区
        self.epcDisplayBufText = ''
        self.epcDisplayBufTextLineNum = 0



